using JGSY.CMS.LowCode.Platform.Domain.Entities;

namespace JGSY.CMS.LowCode.Platform.Application.Interfaces
{
    /// <summary>
    /// 审计日志服务接口
    /// </summary>
    public interface IAuditService
    {
        // 审计日志记录
        Task LogAsync(AuditLogEntry entry);
        Task LogAsync(string tenantId, string action, string resourceType, string? resourceId = null, 
                     object? oldValues = null, object? newValues = null, string? userId = null, 
                     string? description = null, string riskLevel = "Low");
        Task LogBatchAsync(List<AuditLogEntry> entries);
        
        // 审计日志查询
        Task<AuditPagedResult<AuditLog>> GetLogsAsync(string tenantId, AuditLogFilter filter);
        Task<AuditLog?> GetLogAsync(Guid logId);
        Task<List<AuditLog>> GetUserLogsAsync(string tenantId, string userId, DateTime? startDate = null, DateTime? endDate = null);
        Task<List<AuditLog>> GetResourceLogsAsync(string tenantId, string resourceType, string resourceId, DateTime? startDate = null, DateTime? endDate = null);
        
        // 审计统计
        Task<AuditStatistics> GetStatisticsAsync(string tenantId, DateTime? startDate = null, DateTime? endDate = null);
        Task<List<AuditActivity>> GetActivitySummaryAsync(string tenantId, DateTime date, string groupBy = "hour");
        Task<List<TopUserActivity>> GetTopUsersAsync(string tenantId, DateTime? startDate = null, DateTime? endDate = null, int count = 10);
        Task<List<RiskOperation>> GetRiskOperationsAsync(string tenantId, string riskLevel = "High", DateTime? startDate = null, DateTime? endDate = null);
        
        // 规则管理
        Task<AuditRule> CreateRuleAsync(AuditRule rule);
        Task<AuditRule> UpdateRuleAsync(AuditRule rule);
        Task DeleteRuleAsync(Guid ruleId);
        Task<List<AuditRule>> GetRulesAsync(string tenantId);
        Task<AuditRule?> GetRuleAsync(Guid ruleId);
        Task<bool> ToggleRuleAsync(Guid ruleId, bool enabled);
        
        // 配置管理
        Task<AuditConfiguration> GetConfigurationAsync(string tenantId);
        Task<AuditConfiguration> UpdateConfigurationAsync(AuditConfiguration configuration);
        Task ResetConfigurationAsync(string tenantId);
        
        // 报告生成
        Task<AuditReport> CreateReportAsync(string tenantId, AuditReportRequest request);
        Task<List<AuditReport>> GetReportsAsync(string tenantId);
        Task<AuditReport?> GetReportAsync(Guid reportId);
        Task<byte[]> DownloadReportAsync(Guid reportId);
        Task DeleteReportAsync(Guid reportId);
        Task<string> GenerateComplianceReportAsync(string tenantId, string complianceStandard, DateTime startDate, DateTime endDate);
        
        // 数据清理
        Task<int> CleanupExpiredLogsAsync(string tenantId);
        Task<int> CleanupLogsByDateAsync(string tenantId, DateTime beforeDate);
        Task<int> ArchiveOldLogsAsync(string tenantId, DateTime beforeDate);
        
        // 导入导出
        Task<byte[]> ExportLogsAsync(string tenantId, AuditLogFilter filter, string format = "xlsx");
        Task<ImportResult> ImportLogsAsync(string tenantId, Stream fileStream, string format = "xlsx");
        
        // 实时监控
        Task StartRealTimeMonitoringAsync(string tenantId);
        Task StopRealTimeMonitoringAsync(string tenantId);
        Task<List<RealTimeAlert>> GetRealTimeAlertsAsync(string tenantId);
        
        // 合规性检查
        Task<ComplianceReport> CheckComplianceAsync(string tenantId, string standard, DateTime? startDate = null, DateTime? endDate = null);
        Task<List<ComplianceGap>> GetComplianceGapsAsync(string tenantId, string standard);
        Task<bool> IsCompliantAsync(string tenantId, string standard);
    }

    /// <summary>
    /// 审计日志条目 (用于记录)
    /// </summary>
    public class AuditLogEntry
    {
        public string TenantId { get; set; } = string.Empty;
        public string? UserId { get; set; }
        public string? UserName { get; set; }
        public string Action { get; set; } = string.Empty;
        public string ResourceType { get; set; } = string.Empty;
        public string? ResourceId { get; set; }
        public string? ResourceName { get; set; }
        public string? Description { get; set; }
        public object? OldValues { get; set; }
        public object? NewValues { get; set; }
        public string? IpAddress { get; set; }
        public string? UserAgent { get; set; }
        public DateTime Timestamp { get; set; } = DateTime.UtcNow;
        public string Result { get; set; } = "Success";
        public string? ErrorMessage { get; set; }
        public int? Duration { get; set; }
        public Dictionary<string, object>? Metadata { get; set; }
        public string RiskLevel { get; set; } = "Low";
        public string? SessionId { get; set; }
    }

    /// <summary>
    /// 审计日志筛选器
    /// </summary>
    public class AuditLogFilter
    {
        public string? UserId { get; set; }
        public string? UserName { get; set; }
        public string? Action { get; set; }
        public string? ResourceType { get; set; }
        public string? ResourceId { get; set; }
        public DateTime? StartDate { get; set; }
        public DateTime? EndDate { get; set; }
        public string? Result { get; set; }
        public string? RiskLevel { get; set; }
        public string? IpAddress { get; set; }
        public string? SearchText { get; set; }
        public int PageIndex { get; set; } = 0;
        public int PageSize { get; set; } = 50;
        public string SortBy { get; set; } = "Timestamp";
        public bool SortDescending { get; set; } = true;
    }

    /// <summary>
    /// 分页结果
    /// </summary>
    public class AuditPagedResult<T>
    {
        public List<T> Items { get; set; } = new();
        public int TotalCount { get; set; }
        public int PageIndex { get; set; }
        public int PageSize { get; set; }
        public int TotalPages => (int)Math.Ceiling((double)TotalCount / PageSize);
        public bool HasPrevious => PageIndex > 0;
        public bool HasNext => PageIndex + 1 < TotalPages;
    }

    /// <summary>
    /// 审计统计
    /// </summary>
    public class AuditStatistics
    {
        public int TotalLogs { get; set; }
        public int TodayLogs { get; set; }
        public int SuccessfulOperations { get; set; }
        public int FailedOperations { get; set; }
        public int HighRiskOperations { get; set; }
        public int UniqueUsers { get; set; }
        public int UniqueResources { get; set; }
        public Dictionary<string, int> TopActions { get; set; } = new();
        public Dictionary<string, int> TopResourceTypes { get; set; } = new();
        public Dictionary<string, int> TopUsers { get; set; } = new();
    }

    /// <summary>
    /// 审计活动汇总
    /// </summary>
    public class AuditActivity
    {
        public DateTime Period { get; set; }
        public int Count { get; set; }
        public int SuccessCount { get; set; }
        public int FailedCount { get; set; }
        public int HighRiskCount { get; set; }
    }

    /// <summary>
    /// 用户活动排行
    /// </summary>
    public class TopUserActivity
    {
        public string UserId { get; set; } = string.Empty;
        public string UserName { get; set; } = string.Empty;
        public int ActivityCount { get; set; }
        public DateTime LastActivity { get; set; }
        public int RiskScore { get; set; }
    }

    /// <summary>
    /// 风险操作
    /// </summary>
    public class RiskOperation
    {
        public Guid Id { get; set; }
        public string Action { get; set; } = string.Empty;
        public string ResourceType { get; set; } = string.Empty;
        public string? UserId { get; set; }
        public string? UserName { get; set; }
        public string RiskLevel { get; set; } = string.Empty;
        public DateTime Timestamp { get; set; }
        public string? Description { get; set; }
    }

    /// <summary>
    /// 审计报告请求
    /// </summary>
    public class AuditReportRequest
    {
        public string Name { get; set; } = string.Empty;
        public string Type { get; set; } = "Summary";
        public string? Description { get; set; }
        public DateTime StartDate { get; set; }
        public DateTime EndDate { get; set; }
        public AuditLogFilter? Filter { get; set; }
        public string Format { get; set; } = "PDF";
        public bool IncludeDetails { get; set; } = true;
        public bool IncludeCharts { get; set; } = true;
    }

    /// <summary>
    /// 导入结果
    /// </summary>
    public class ImportResult
    {
        public int TotalRecords { get; set; }
        public int SuccessfulRecords { get; set; }
        public int FailedRecords { get; set; }
        public List<string> Errors { get; set; } = new();
    }

    /// <summary>
    /// 实时告警
    /// </summary>
    public class RealTimeAlert
    {
        public Guid Id { get; set; }
        public string Type { get; set; } = string.Empty;
        public string Title { get; set; } = string.Empty;
        public string Message { get; set; } = string.Empty;
        public string Severity { get; set; } = "Low";
        public DateTime Timestamp { get; set; }
        public bool IsResolved { get; set; }
        public Dictionary<string, object>? Context { get; set; }
    }

    /// <summary>
    /// 合规报告
    /// </summary>
    public class ComplianceReport
    {
        public string Standard { get; set; } = string.Empty;
        public bool IsCompliant { get; set; }
        public double ComplianceScore { get; set; }
        public List<ComplianceCheck> Checks { get; set; } = new();
        public List<ComplianceGap> Gaps { get; set; } = new();
        public DateTime GeneratedAt { get; set; }
    }

    /// <summary>
    /// 合规检查项
    /// </summary>
    public class ComplianceCheck
    {
        public string Name { get; set; } = string.Empty;
        public string Description { get; set; } = string.Empty;
        public bool Passed { get; set; }
        public string? Evidence { get; set; }
        public string? Recommendation { get; set; }
    }

    /// <summary>
    /// 合规缺口
    /// </summary>
    public class ComplianceGap
    {
        public string Requirement { get; set; } = string.Empty;
        public string Description { get; set; } = string.Empty;
        public string Severity { get; set; } = "Medium";
        public string Recommendation { get; set; } = string.Empty;
        public DateTime? DueDate { get; set; }
    }
}
